package com.weitu.permission.dataAuthority.service.impl;

import com.weitu.base.entity.Tree;
import com.weitu.permission.dataAuthority.entity.DataAuthority;
import com.weitu.permission.dataAuthority.mapper.DataAuthorityMapper;
import com.weitu.permission.dataAuthority.service.DataAuthorityService;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.mapper.DepartmentMapper;
import com.weitu.permission.role.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/3/21.
 */
@Service
public class DataAuthorityServiceImpl implements DataAuthorityService {
    private final DataAuthorityMapper dataAuthorityMapper;
    private final DepartmentMapper departmentMapper;

    @Autowired
    public DataAuthorityServiceImpl(DataAuthorityMapper dataAuthorityMapper, DepartmentMapper departmentMapper) {
        this.dataAuthorityMapper = dataAuthorityMapper;
        this.departmentMapper = departmentMapper;
    }

    @Override
    public List<Role> queryRoleList(Map<String, Object> map) {
        return this.dataAuthorityMapper.queryRoleList(map);
    }

    @Override
    public int queryCount(Map<String, Object> map) {
        return this.dataAuthorityMapper.queryCount(map);
    }

    @Override
    public List<DataAuthority> queryDataAuthorityList(Map<String, Object> map) {
        return this.dataAuthorityMapper.queryDataAuthorityList(map);
    }

    @Override
    @Transactional
    public boolean insertDataAuthority(List<DataAuthority> dataAuthorityList) {
        int count = dataAuthorityList.size();
        int result = this.dataAuthorityMapper.insertDataAuthority(dataAuthorityList);
        return count == result;
    }

    @Override
    @Transactional
    public boolean deleteDataAuthority(Map<String, Object> map) {
        int count = this.dataAuthorityMapper.deleteDataAuthority(map);
        return count > 0;
    }

    @Override
    @Transactional
    public boolean updateDataAuthority(String deptId, String roleId, List<DataAuthority> dataAuthorityList) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("deptId", deptId);
        map.put("roleId", roleId);
        deleteDataAuthority(map);
        if (dataAuthorityList.size() == 0) {
            return true;
        }
        return insertDataAuthority(dataAuthorityList);
    }

    @Override
    public List<Tree> getDataAuthorityDepartment(Map<String, Object> paramMap, String parentId) {
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(parentId);
        paramMap.put("parentId", parentId);
        List<Department> departmentCheckedList = this.dataAuthorityMapper.queryDataAuthorityDepartmentList(paramMap);
        List<Department> childrenDepartmentList;
        List<Tree> parentTreeList = new ArrayList<Tree>();
        Tree tree;

        for (Department department : parentDepartmentList) {
            String id = department.getId();
            String name = department.getName();
            String code = department.getCode();
            Map map = new HashMap();
            map.put("code", code);
            tree = new Tree();
            tree.setId(id);
            tree.setText(name);
            tree.setAttributes(map);
            for (Department departmentChecked : departmentCheckedList) {
                if (id.equals(departmentChecked.getId())) {
                    tree.setChecked("true");
                }
            }
            childrenDepartmentList = this.departmentMapper.findByParentId(id);
            if (childrenDepartmentList.size() > 0) {
                paramMap.put("parentId", id);
                tree.setChildren(getDataAuthorityDepartment(paramMap, id));
            }
            parentTreeList.add(tree);

        }
        return parentTreeList;
    }

    @Override
    public List<String> selectDataDeptIdByUserIdAndDeptId(String userId, String deptId) {
        return this.dataAuthorityMapper.selectDataDeptIdByUserIdAndDeptId(userId, deptId);
    }
}
